package com.buildings.util;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.WindowManager;

import com.buildings.activity.ImgFileListActivity;
import com.buildings.base.BaseApplication;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;

;

public class HYDialogUtils {

    /**
     * 创建加载dialog
     *
     * @param context
     * @return
     */
    public static Dialog createProgressDialog(Context context) {
        return createProgressDialog(context, true);
    }

    /**
     * 创建加载dialog
     *
     * @param context
     * @param needCancle
     * @return
     */
    public static Dialog createProgressDialog(Context context, boolean needCancle) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setMessage("加载中...");
        dialog.setCancelable(needCancle);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    public static Dialog createProgressDialogMSG(Context context, String msg, boolean needCancle) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setMessage(msg);
        dialog.setCancelable(needCancle);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }



    /**
     * 相册选择
     */
    public static void showImagePickDialog(final Activity context, final int count) {
        HYImageUtils.showImagePickDialog(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                switch (which) {
                    case 0:
                        HYImageUtils.pickImageFromCamera(context);
                        break;
                    case 1:
                        Intent intent = new Intent(context, ImgFileListActivity.class);
                        intent.putExtra("count", count);
                        context.startActivityForResult(intent, HYImageUtils.REQUEST_CODE_FROM_ALBUM);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * 相册选择
     */
    public static void showImagePickDialogNew(final Activity context, final int count, Uri imageUriFromCamera, int requestCodeA, int requestCodeC) {
        HYImageUtils.showImagePickDialog(context, (dialog, which) -> {
            dialog.dismiss();
            switch (which) {
                case 0:
                    Intent intent = new Intent();
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUriFromCamera);
                    if (intent.resolveActivity(context.getPackageManager()) != null) {
                        context.startActivityForResult(intent, requestCodeA);
                    }
                    break;
                case 1:
                    Intent intent1 = new Intent(context, ImgFileListActivity.class);
                    intent1.putExtra("count", count);
                    context.startActivityForResult(intent1, requestCodeC);
                    break;
                default:
                    break;
            }
        });
    }

    /**
     * 普通警告提示框, 有确定取消按钮
     *
     * @param content         提示内容
     * @param onClickListener 确认回调
     */
    public static void showCommonAlertDialog(Context context, String content, DialogInterface.OnClickListener onClickListener) {
        new AlertDialog.Builder(context)
                .setMessage(content)
                .setPositiveButton("确定", onClickListener)
                .show();
    }

    /**
     * 普通警告提示框, 有确定取消按钮
     *
     * @param content                 提示内容
     * @param positiveonClickListener 确认回调
     */
    public static void showCommonAlertDialog(Context context, String content, String positiveStr, String negativeStr,
                                             DialogInterface.OnClickListener positiveonClickListener
            , DialogInterface.OnClickListener negativeClickListener) {
        new AlertDialog.Builder(context)
                .setMessage(content)
                .setNegativeButton(negativeStr, negativeClickListener)
                .setPositiveButton(positiveStr, positiveonClickListener)
                .show();
    }

    public static void showCommonAlertDialogNotTouchOuside(Context context, String content, String positiveStr, String negativeStr,
                                                           DialogInterface.OnClickListener positiveonClickListener
            , DialogInterface.OnClickListener negativeClickListener) {
        AlertDialog dialog;
        dialog = new AlertDialog.Builder(context)
                .setMessage(content)
                .setNegativeButton(negativeStr, negativeClickListener)
                .setPositiveButton(positiveStr, positiveonClickListener)
                .create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

    }

    /**
     * 有标题的提示框, 有确定取消按钮
     *
     * @param content                 提示内容
     * @param positiveonClickListener 确认回调
     */
    public static void showTitleCommonAlertDialog(Context context, String title, String content, String positiveStr, String negativeStr,
                                                  DialogInterface.OnClickListener positiveonClickListener, DialogInterface.OnClickListener negativeClickListener) {
        new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(content)
                .setNegativeButton(negativeStr, negativeClickListener)
                .setPositiveButton(positiveStr, positiveonClickListener)
                .show();
    }


    /**
     * 显示全局dialog
     *
     * @param context
     * @param content
     * @param onClickListener
     */
    public static void showCommonWINDOWAlertDialog(Context context, String content, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(BaseApplication.getInstance().getBaseContext())
                .setMessage(content)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", onClickListener);
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();
    }

    /**
     * 普通Item选择提示框
     *
     * @param context
     * @param items
     * @param onClickListener
     */
    public static void showItemAlertDialog(Context context, String[] items,
                                           DialogInterface.OnClickListener onClickListener) {
        AlertDialog dialog = new AlertDialog.Builder(context).
                setItems(items, onClickListener).create();
//        Window window = dialog.getWindow();
//        window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
//        window.setWindowAnimations(R.style.mystyle);  //添加动画
        dialog.show();
    }

    /**
     * 单选dialog
     *
     * @param context
     * @param title
     * @param checkedItem
     * @param items
     * @param onClickListener
     */
    public static void showSingleChoiceItemAlertDialog(Context context, String title, int checkedItem
            , String[] items, DialogInterface.OnClickListener onClickListener) {
        new AlertDialog.Builder(context)
                .setTitle(title)
                .setSingleChoiceItems(items, checkedItem, onClickListener)
//                .setPositiveButton("确定", onClickListener)
//                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                })
                .show();

    }

    /**
     * 多选dialog
     *
     * @param context
     * @param title
     * @param checkedItem
     * @param items
     * @param onClickListener
     */
    public static AlertDialog alertDialog3;

    public static void showMutilAlertDialog(Activity acitivity, String title, String[] items,DialogCakkBack dialogCakkBack) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(acitivity);
        alertDialogBuilder.setTitle(title);
        alertDialogBuilder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                // dialog：不常使用，弹出框接口
                // which：勾选或取消的是第几个
                // isChecked：是否勾选
                if (isChecked) {
                    arrayList.add(which);
                } else {
                    arrayList.remove(which);
                }
            }
        });
        alertDialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                if (dialogCakkBack != null){
                    dialogCakkBack.callBack(arrayList);
                }
                alertDialog3.dismiss();
            }
        });
        alertDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                alertDialog3.dismiss();
            }
        });
        alertDialog3 = alertDialogBuilder.create();
        alertDialog3.show();
    }


    public interface DialogCakkBack {
        void callBack(List<Integer> list);
    }
}
